<template>
  <!-- 页面-岗位管理 -->
  <div class="run-mod-box innerPageTable-box-OneTwo" style=" overflow: auto;">
    <!-- 搜索区域 -->
    <SelfAdaption>
      <Form
        :model="searchForm"
        ref="postFormSearch"
        slot="adaption-bar"
        label-position="top"
        class="ivu-form-no-margin-bottom"
        inline
      >
        <template> 
          <!-- 岗位名称 -->
          <Form-item prop="postName" :label="$t('module.post.postName')">
            <Input
              type="text"
              :maxlength="30"
              v-model.trim="searchForm.postName"
              :placeholder="$t('module.post.warning.enterPostName')"
              
              @on-enter="search"
            ></Input>
          </Form-item>
          
          <!-- 岗位编码 -->
          <Form-item prop="postCode" :label="$t('module.post.postCode')">
            <Input
              type="text"
              :maxlength="30"
              v-model.trim="searchForm.postCode"
              :placeholder="$t('module.post.warning.enterPostCode')"
              
              @on-enter="search"
            ></Input>
          </Form-item>
          
          <!-- 查询\重置按钮 -->
          <Form-item prop="button" class="search-solt-btn">  
            <RPButton optType="search" @click="search" />
            <RPButton optType="reset" @click="reset" />
          </Form-item>
        </template>
      </Form>
    </SelfAdaption>
    <InnerPageTable ref="postTable" :config="tableConfig">
      <!-- handler bar 查询/重置-->
      <!--
      <div slot="handle-bar">
        <RPButton optType="search" @click="search" />
        <RPButton optType="reset" @click="reset" />
      </div>
      -->
      <!-- table bar 添加-->
      <div slot="table-bar">
        <RPButton optType="create" @click="add" />
        <RPButton optType="delete" @click="deleted()" />
      </div>
    </InnerPageTable>
  </div>
</template>
<script>
import PostEdit from "./PostEdit.vue";
import PostAssignRole from "./PostAssignRole.vue";
import PostAssignUser from "./PostAssignUser.vue";
import postAPI from "@/api/postApi.js";
import { postManageColumns } from "./columns.js";
import { CoreModuleName } from "@/const.js";
import locales from "./locales.js";
import valueSetAPI from "@/api/valueSetAPI.js";
import tenantAPI from "@/api/tenantApi.js";

export default {
  name: "post-manage",
  mixins: [locales],
  data() {
    return {
      currentPost: null,
      productList: [],
      tenantList: [],
      searchForm: {
        adminId: "",
        productId: "",
        appId: "",
        tenantCode: "",
        treeNodeId: "",
        postName: "",
        postCode: ""
      },
      tableConfig: {
        loadDataFn: ({ responseProcess, param }) => {
          postAPI.queryPostList(param).then(data => {
            let { records } = data;
            records.forEach(post => {
              post.productName = this.getProductName(post.productId);
              post.tenantName = this.getTenantName(post.tenantCode);
            });
            responseProcess(data);
          });
        },
        columnsFn: postManageColumns.bind(this),
        autoFirst: false
      }
    };
  },
  computed: {},
  created: function() {
    this.init();
  },
  mounted: function() {},
  methods: {
    init() {
      this.currentPost = $store.state[CoreModuleName].currentPost;
      if (!this.currentPost) return;

      // 根据当前岗位查询数据
      this.searchForm.productId = this.currentPost.productId;
      this.searchForm.tenantCode = this.currentPost.tenantCode;
      this.searchForm.treeNodeId = this.currentPost.treeNodeId;
      this.initProduct();
      this.initTenant();
    },
    // 初始化所属产品
    initProduct() {
      valueSetAPI.getValueSetByCode("RC_PRODUCT").then(data => {
        for (const product of data) {
          this.productList[product.vsiKey] = product.vsiValue;
        }
        this.search(1);
      });
    },
    // 初始化所属租户
    initTenant() {
      tenantAPI.queryAllTenantList().then(data => {
        for (const tenant of data) {
          this.tenantList[tenant.tenantCode] = tenant.tenantName;
        }
      });
    },
    getProductName(productId) {
      if (productId) {
        return this.productList[productId] ? this.productList[productId] : productId;
      }
      return productId;
    },

    getTenantName(tenantCode) {
      if (tenantCode) {
        return this.tenantList[tenantCode] ? this.tenantList[tenantCode] : tenantCode;
      }
      return tenantCode;
    },
    search(page) {
      this.$refs["postTable"].load(this.searchForm, page);
    },
    reset() {
      this.$refs["postFormSearch"].resetFields();
      this.search(1);
    },
    add() {
      new this.$pageModal(
        PostEdit,
        {
          props: {
            adminProductId: this.currentPost.productId,
            tenantCode: this.currentPost.tenantCode,
            rootOrgId: this.currentPost.treeNodeId,
            rootOrgName: this.currentPost.treeNodeName
          }
        },
        data => {
          if (data) {
            this.search();
          }
        }
      );
    },
    edit(postId) {
      new this.$pageModal(
        PostEdit,
        {
          props: {
            postId: postId,
            adminProductId: this.currentPost.productId,
            tenantCode: this.currentPost.tenantCode,
            rootOrgId: this.currentPost.treeNodeId,
            rootOrgName: this.currentPost.treeNodeName
          }
        },
        data => {
          if (data) {
            this.search();
          }
        }
      );
    },
    deleted() {
      let selectedIds = this.getSelectedIds();
      if (selectedIds.length == 0) {
        this.$message.error(this.$t("module.post.warning.selectPost"));
        return;
      }
      new $confirm(this.$t("module.post.warning.confirmToDelete"), this).then(() => {
        postAPI
          .deletePostById(selectedIds)
          .then(res => {
            this.$message.success({
              content: this.$t("module.orgManage.deleteSuccess"),
              duration: 3
            });
            this.search();
          })
          .catch(data => {
            this.$message.error({
              content: data.message,
              duration: 3
            });
          });
      });
    },
    assignRole(post) {
      new this.$pageModal(
        PostAssignRole,
        {
          props: {
            postId: post.id,
            postName: post.postName,
            postCode: post.postCode,
            productId: post.productId,
            treeNodeId: post.treeNodeId,
            appId: post.appId
          }
        },
        data => {}
      );
    },
    assignUser(post) {
      new this.$pageModal(
        PostAssignUser,
        {
          props: {
            postId: post.id,
            postName: post.postName,
            postCode: post.postCode,
            productId: post.productId || "",
            appId: post.appId,
            tenantCode: post.tenantCode || ""
          }
        },
        data => {}
      );
    },
    getSelectedIds() {
      let list = this.$refs["postTable"].getSelectioned() || [];
      let ids = [];
      for (let u of list) {
        ids.push(u.id);
      }
      return ids;
    },
    getDynamicCol() {
      let dynamicCol = [];
      dynamicCol.push({
        title: this.$t("module.post.tenantCode"),
        key: "tenantName",
        width: 160,
        render: (h, params) => {
          return h(
            "div",
            {
              attrs: {
                class: "ivu-table-cell ivu-table-cell-ellipsis",
                title: params.row.tenantName
              }
            },
            params.row.tenantName
          );
        }
      });
      if (!!window.RPConfig.orgTreeCode) {
        dynamicCol.push({
          title: this.$t("module.post.orgName"),
          key: "treeNodeName",
          width: 180,
          render: (h, params) => {
            return h(
              "div",
              {
                attrs: {
                  class: "ivu-table-cell ivu-table-cell-ellipsis",
                  title: params.row.treeNodeName
                }
              },
              params.row.treeNodeName
            );
          }
        });
      }
      return dynamicCol;
    }
  },
  watch: {},
  directives: {}
};
</script>
